package com.coffee.wx.uitls;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.util.ImageUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class BaiDuAiUtils {

    //设置APPID/AK/SK
    public static final String APP_ID = "61880769";
    public static final String API_KEY = "SD8th49sg8AL8mvfySxLM2bd";
    public static final String SECRET_KEY = "6WPxHa0I84rVnWr7g3qydhzit3xcZL79";

    /**
     * {
     * "log_id": 2471272194,
     * "words_result_num": 2,
     * "words_result":
     * [
     * {"words": " TSINGTAO"},
     * {"words": "青島睥酒"}
     * ]
     * }
     */
    public static String textRecognition(String url) {

        if (url == null) {
            return "文字识别失败";
        }

        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        JSONObject res = client.basicGeneralUrl(url, new HashMap<>());

        JSONArray wordsResult = res.getJSONArray("words_result");

        StringBuilder resultMsg = new StringBuilder();
        for (Object o : wordsResult) {
            JSONObject jsonObject = (JSONObject) o;
            resultMsg.append(jsonObject.getString("words")).append(" ");
        }
        return resultMsg.toString();
    }

}
